#!/usr/bin/python

import pprint
from sys import argv

# Some globals
diceSides = 10
debug = True

pp = pprint.PrettyPrinter(indent=4)

def getProbDicts(confDict):

    # Obtain a dict of all possible outcomes
    outcomesDict = getOutcomesDict(confDict["numDice"])

    # Walk the probability dict to determine the success against
    #   a TN
    walkOutcomes(outcomesDict, confDict["tn"])

def walkOutcomes(outcomesDict, targetNum):
   
    outcomes = float(len(outcomesDict))
    successDict = {} 
    for x in outcomesDict:
#        print "Checking x [%s]" % x
        # x is a list
        successCount = 0
        for y in x:
 #           print "Checking value %i + %i = %i" % (y, diceMod, y+diceMod)
            if y > targetNum:
                successCount = successCount+1

 #       print "Raw success count is: %i" % successCount 

        if successCount in successDict:
            successDict[successCount].append(x)
        else:
            successDict[successCount] = [x]

    for x in successDict.iterkeys():
        succCount = len(successDict[x])
        succPerc = ((succCount) / outcomes ) * 100.0
        print "[%s] success were generated [%s] times, a %s%% chance." % (x, succCount, succPerc)
#        if debug:
 #           for y in successDict[x]:
  #              print "\t%s" % y

def getOutcomesDict(numDice):
    outcomes = []

    for x in range(1, numDice+1):
        outcomes = appendDiceSides(outcomes, diceSides)

#    if debug:
#        pp.pprint(outcomes)

    return outcomes

def appendDiceSides(diceList, diceSides):

    if len(diceList) is 0:
        for x in range(1, diceSides+1):
            diceList.append([x])
        return diceList

    newList = []
    for y in diceList:
        # y is an element of list type
        for x in range(1, diceSides+1):
            z = y[:]
            z.append(x)
            newList.append(z)

    return newList

if __name__ == '__main__':
    confDict = {}
    numDice = 3
    if len(argv) >= 2:
        numDice = int(argv[1])
        confDict["numDice"] = numDice

    defense = 0
    if len(argv) >= 3:
        defense = int(argv[2])
        confDict["def"] = defense

    attack = 0
    if len(argv) >= 4:
        attack = int(argv[3])
        confDict["atk"] = attack

    targetNum = defense - attack
    confDict["tn"] = targetNum

    print "Generating (%iD%i) vs. (%i - %i) = %i" % (numDice, diceSides, defense, attack, targetNum)
    probDict = getProbDicts(confDict)

